package com.glorysoft.tonglingxjpda.ui.home.viewmodel

import android.app.Activity
import androidx.databinding.ObservableArrayList
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseViewModel
import com.glorysoft.tonglingxjpda.utils.MyUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.io.InputStream

/**
 * FileName: HomeViewModel
 * Author: XJH
 * Date: 2021/7/12 17:20
 * Description:
 * History:
 */
class HomeViewModel: BaseViewModel() {

    var mfgItems = ObservableArrayList<FreeUI_TabIndexGridEntity>()
    val START_ACTIVITY = 0
    val MFGLAYOUT = 1

    private val mainItemClick = object : OnItemClickListener {
        override fun onItemClick(item: FreeUI_TabIndexGridEntity) {
            defUI.callObserve.value = CallObserveBean(START_ACTIVITY, item)
        }
    }

    var itemBinding = ItemBinding.of<FreeUI_TabIndexGridEntity>(BR.tabIndexGrid, R.layout.item_main)
        .bindExtra(BR.itemClickListener, mainItemClick)

    fun initAssets(context : Activity){
//        val mfgJsonStream: InputStream = context.resources.assets.open("mfg.txt")  //铜陵，天津
        val mfgJsonStream: InputStream = context.resources.assets.open("dlmfg.txt")  //dalian
//        val mfgJsonStream: InputStream = context.resources.assets.open("gzmfg.txt")  //广州
        val mfgJson = FreeApi_StringUtils.getString(mfgJsonStream)
        val mfgEntity :List<FreeUI_TabIndexGridEntity> = Gson().fromJson(mfgJson, object : TypeToken<List<FreeUI_TabIndexGridEntity?>?>() {}.type)
        if (mfgEntity.isEmpty()) {
            defUI.callObserve.value = CallObserveBean(MFGLAYOUT, "")
        }else{
            mfgItems.addAll(MyUtils.setPicToDrawable(context,mfgEntity))
        }
    }


    interface OnItemClickListener {
        fun onItemClick(item: FreeUI_TabIndexGridEntity)
    }
}